package gemini.castor.ui.client.page.content.customer.customerlist.edit;

import gemini.basic.model.Address;
import gemini.basic.model.Customer;
import gemini.basic.model.Distributor;
import gemini.basic.model.Person;
import gemini.castor.ui.client.constants.I18nConstants;
import gemini.castor.ui.client.page.widget.ErrorWidget;
import gemini.castor.ui.client.page.widget.address.AddressObject;
import gemini.castor.ui.client.page.widget.address.AddressWidget;
import gemini.castor.ui.client.page.widget.date.DateWidget;
import gemini.castor.ui.client.utils.GuiUtils;
import gemini.castor.ui.client.utils.UiConstants;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.uibinder.client.UiTemplate;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

public class CustomerEditView extends Composite implements
		CustomerEditPresenter.Display {

	@UiTemplate("CustomerEditView.ui.xml")
	interface MyUiBinder extends UiBinder<Panel, CustomerEditView> {
	}
	
	private I18nConstants constants;
	private static final MyUiBinder binder = GWT.create(MyUiBinder.class);

	public CustomerEditView() {
		initWidget(binder.createAndBindUi(this));
		constants = GWT.create(I18nConstants.class);
		loadData();
	}

	@UiField
	HTML htmTitle;

	@UiField
	HTML htmInformation;

	@UiField
	AddressWidget awgAddress;

	@UiField
	ListBox lbxLanguage;
	
	@UiField
	ListBox lbxSex;
	
	@UiField
	TextBox tbxEmail;
	
	@UiField
	TextBox tbxPhone;
	
	@UiField
	TextBox tbxLastName;
	
	@UiField
	TextBox tbxFirstName;
	
	@UiField
	DateWidget dwgBirthDay;

	@UiField
	Button btnSubmit;

	@UiField
	Button btnBack;

	@UiField
	Button btnReset;
	
	@UiField
	ErrorWidget cnwError;

	@UiHandler("btnReset")
	void resetFields(ClickEvent event) {
		loadForm();
	}

	@Override
	public void initCustom() {
		if (this.customer == null) {
			htmTitle.setHTML("<h1>"+ constants.htmNewCustomer() +"</h1>");
			htmInformation
					.setHTML(constants.htmNewCustomerInfo());
		} else {
			htmTitle.setHTML("<h1>"+ constants.htmUpdateCustomer() +"</h1>");
			htmInformation.setHTML(constants.htmUpdateCustomerInfo());
		}
	}

	@Override
	public Widget asWidget() {
		return this;
	}

	private Customer customer;

	@Override
	public HasClickHandlers getBackClickHandlers() {
		return btnBack;
	}

	@Override
	public HasClickHandlers getSubmitClickHandlers() {
		return btnSubmit;
	}

	@Override
	public void setCustomer(Customer customer) {
		this.customer = customer;
	}

	@Override
	public void loadForm() {
		cnwError.setVisible(false);
		if (this.customer == null) {
			dwgBirthDay.reset();
			awgAddress.reset();
			lbxLanguage.setSelectedIndex(0);
			lbxSex.setSelectedIndex(0);
			tbxEmail.setValue("");
			tbxPhone.setValue("");
			tbxLastName.setValue("");
			tbxFirstName.setValue("");
			
		} else {
			dwgBirthDay.setDate(this.customer.getPersons().get(0).getDateOfBirth());
			awgAddress.setAddress(this.customer.getPersons().get(0).getAddress());
			lbxLanguage.setSelectedIndex(0);
			lbxSex.setSelectedIndex(0);
			tbxEmail.setValue("");
			tbxPhone.setValue("");
			tbxLastName.setValue("");
			tbxFirstName.setValue("");
		}
	}

	@Override
	public Customer getCustomer() {
		if (customer == null) {
			customer = new Customer();
		}
		awgAddress.bind();
		Person person = new Person();
		person.setFirstName(tbxFirstName.getValue());
		person.setLastName(tbxLastName.getValue());
		person.setAddress(createAddressFromAddressObject(addressObject));
		person.setEmail(tbxEmail.getValue());
		person.setDateOfBirth(dwgBirthDay.getDateFromDateWidget());
		person.setPhoneNumber(tbxPhone.getValue());
		person.setSex(lbxSex.getValue(lbxSex.getSelectedIndex()));
		person.setLanguageCode(lbxLanguage.getValue(lbxLanguage.getSelectedIndex()));
		List<Person> persons = new ArrayList<Person>();
		persons.add(person);
		customer.setPersons(persons);
		
		Distributor distributor = new Distributor();
		customer.setDistributor(distributor);

		return customer;
	}
	
	@Override
	public boolean isValidate(){
		boolean result = awgAddress.isValidate();
		if(!result){
			cnwError.setVisible(true);
		} else {
			cnwError.setVisible(false);
		}
		return result;
	}

	private Address createAddressFromAddressObject(AddressObject addressObject){
		Address address = new Address();
		address.setAddressLine1(addressObject.getLine1());
		address.setAddressLine2(addressObject.getLine2());
		address.setCity(addressObject.getCity());
		address.setCountry(addressObject.getCountry());
		address.setDistrict(addressObject.getDistrict());
		return address;
	}
	
	private AddressObject addressObject;
	
	public void loadData() {
		addressObject = new AddressObject();
		awgAddress.setAddressObject(addressObject);
		cnwError.setMessage(constants.emptyField());
		tbxPhone.setMaxLength(UiConstants.MAX_LEN_PHONE);
		
		String sex[] = (constants.stringSex()).split(",");

		for (String item : sex) {
			String[] sexValue = item.split(":");
			lbxSex.addItem(sexValue[0],sexValue[1]);
		}
		
		Map<String,String> map = GuiUtils.getValueFromConstant(constants.stringLanguage());
		for (String key : map.keySet()) {
			lbxLanguage.addItem(map.get(key),key);
		}
	}
}
